<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-07-03 09:47
 *
 * Version: 2.0
 */

namespace App\Models;


use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class UserPortfolio extends Model
{
	use SoftDeletes;
	protected $table ="user_portfolio";
	protected $dates=['deleted_at'];
	protected $guarded = [];

	protected $hidden = [
		"updated_at"
	];

	public function store(){
		return $this->hasOne(Store::class,'id','store_id');
	}

	public function resource()
	{
		return $this->hasOne(UserPortfolioResource::class, 'portfolio_id', 'id');
	}

	public function userPortfolioLike(){
		return $this->hasMany(UserPortfolioLike::class, 'portfolio_id','id');
	}

	public function scopeLikePortfolioByMemberId(Builder $query,$memberId){
		return $query->has('userPortfolioLike','and',function($q) use($memberId){
			$q->where('member_id',$memberId);
		}
		);
	}


	public function user(){
		return $this->belongsTo(User::class,'user_id','id');
	}

	public function storeService(){
		return $this->hasOne(StoreService::class,'id','service_id');
	}

	public function orderItems(){
		return $this->hasMany(OrderItem::class, 'service_id','service_id');
	}


	public function scopeWithCertain($query, $relation, Array $columns){
		return $query->with([$relation => function ($query) use ($columns){
			$query->select(array_merge(['id'], $columns));
		}]);
	}


}